package com.yenlien.traceyun.business.service;

import com.yenlien.traceyun.business.dao.ProductDockDao;
import com.yenlien.traceyun.business.entity.Product;
import com.yenlien.traceyun.business.entity.ProductDock;
import com.yenlien.traceyun.business.service.rest.usercenter.FileRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by LiuSha on 2020/7/1.
 */
@Service
public class ProductDockService {
    @Autowired
    private ProductDockDao productDockDao;
    @Autowired
    private FileRestService fileRestService;

    public Page<ProductDock> findByList(final Product product) {
        Pageable pageable = PageRequest.of(product.getPage(), product.getPageSize(), Sort.Direction.DESC, "createtime");
        return productDockDao.findAll((Root<ProductDock> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
            Path<String> companyId = root.get("companyId");
            //构造查询条件
            List<Predicate> list = new ArrayList<>();
            List<Predicate> or = new ArrayList<>();
            if (!StringUtils.isEmpty(product.getCompanyId())) {
                list.add(cb.equal(companyId, product.getCompanyId()));
            }
            Predicate[] predicate = new Predicate[list.size()];
            Predicate pre_and = cb.and(list.toArray(predicate));
            if (!StringUtils.isEmpty(product.getQuery())) {
                or.add(cb.like(root.get("title"), "%" + product.getQuery() + "%"));
                or.add(cb.like(root.get("relatedProducts"), "%" + product.getQuery() + "%"));
            } else {
                return pre_and;
            }
            Predicate[] pre_or = new Predicate[or.size()];
            Predicate ora = cb.or(or.toArray(pre_or));
            return query.where(pre_and, ora).getRestriction();
        }, pageable);
    }

    public void save(ProductDock productDock, List<MultipartFile> files) throws Exception {
        fileRestService.upload(null, productDock, files);
        productDockDao.save(productDock);
    }


    public void update(ProductDock productDock, List<MultipartFile> files) throws Exception {
        fileRestService.upload(productDockDao.findById(productDock.getId()), productDock, files);
        productDockDao.save(productDock);
    }


    public ProductDock findById(String id) {
        return productDockDao.findById(id);
    }

    @Transactional
    public void delete(String id) {
        productDockDao.deleteById(id);
    }

    public List<ProductDock> findAll(String companyId) {
        if (!StringUtils.isEmpty(companyId)) {
            return productDockDao.findByCompanyId(companyId);
        }
        return productDockDao.findAll();
    }

}
